package globulin;

import globulin.soundFXProccessing.SoundFX;
import jgame.*;
import jgame.platform.JGEngine.*;
import jgame.impl.JGEngineInterface.*;

/** This class defines the bosses in the game.
 * @author Sean Pavlichek
 */
public class Boss extends JGObject {
    // Necessary variable for certain operations

    public Globulin gameScreen;
    // Timers for events
    private int timer = 0;
    private int firingTimer = 0;
    private int dyingTimer = 200;
    // Other necessary variables
    private boolean firingSequenceDone = true;
    private boolean dying = false;
    // Storage for the guns on the boss
    private BossNode bossNode1;
    private BossNode bossNode2;
    private BossNode bossNode3;
    private BossNode bossNode4;

    /** Basic constructor. Creates a new boss with defaults.
     * @param parent The game engine.
     */
    public Boss(Globulin parent) {
        // String name, boolean unique_id, double x, double y, int collisionID,
        // String gfxName, double xSpeed, double ySpeed
        super("boss", true, (parent.getWidth() - 194) / 2, 0, 0, "boss_image", 2, 0);

        // Stores the game screen so that this class has access to its methods
        gameScreen = parent;

        // Creates the boss nodes
        bossNode1 = new BossNode(this, this.x + 7, this.y + 40);
        bossNode2 = new BossNode(this, this.x + 55, this.y + 150);
        bossNode3 = new BossNode(this, this.x + 111, this.y + 150);
        bossNode4 = new BossNode(this, this.x + 161, this.y + 40);
    }

    /** Overrides the default move command.
     */
    public void move() {
        // Increments the timer
        timer += 1;

        // If this is close enough to a wall, reverses the x direction
        if (this.x <= 35 || this.x >= (gameScreen.getWidth() - 230)) {
            this.xspeed = -this.xspeed;
        }

        // If all nodes are dead, activates the death sequence
        if (!bossNode1.isAlive() && !bossNode2.isAlive() && !bossNode3.isAlive() && !bossNode4.isAlive()) {
            dying = true;
            firingSequenceDone = true;
        }

        // If the death sequence is active
        if (dying) {
            // Show random explosions
            new JGObject("explosion", true, eng.random(x, x + 194, 10), eng.random(y, y + 195, 10), 0, "explosion", 0, 0, 32);
            // Play sound effects
            gameScreen.soundFxThreadPool.execute(new SoundFX(gameScreen, "boomfx"));

            // Decrements the timer
            dyingTimer -= 1;

            // If the death sequence is over, notifies the game engine
            if (dyingTimer <= 0) {
                gameScreen.bossAlive = false;
                gameScreen.bossKilled = true;
                // Removes the boss
                this.remove();
            }
            // If the boss is still firing
        } else if (!firingSequenceDone) {
            // Increments the timer
            firingTimer += 1;

            // If the firing timer reaches 4 seconds
            if (firingTimer == 240) {
                // Ends the firing sequence and resets the timer
                firingTimer = 0;
                firingSequenceDone = true;
                bossNode1.ceaseFire();
                bossNode2.ceaseFire();
                bossNode3.ceaseFire();
                bossNode4.ceaseFire();

                // Resets the boss's speed
                this.xspeed = 2;
            }
            // If the timer reaches the 4 second mark and the boss is not firing
        } else if (timer % 240 == 0 && firingSequenceDone) {
            // Opens fire
            firingSequenceDone = false;
            bossNode1.openFire();
            bossNode2.openFire();
            bossNode3.openFire();
            bossNode4.openFire();

            // Sets the boss's speed
            this.xspeed = 5;
        }
    }
}
